package com.ibm.db2oncampus.portal.framework.beans;

import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.ServletException;

/**
 * 
 * @author Wellington
 *
 */
public class ErrorMessageBean extends FacesBeanSupport {

    public ErrorMessageBean() {
    }

    public String getDetails()
    {
        ServletException exception = (ServletException)getRequest().getAttribute("javax.servlet.error.exception");
        if(exception != null)
            return getStackTrace(exception);
        else
            return "N\343o h\341 informa\347\365es de detalhes para o problema encontrado.";
    }

    public String getStackTrace(ServletException exception)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        Throwable cause = exception.getRootCause();
        if(cause == null)
        {
            exception.printStackTrace(pw);
        } else
        {
            pw.println(exception);
            pw.print("Caused by: ");
            cause.printStackTrace(pw);
        }
        return sw.toString();
    }
}